package com.mykt.service;

import com.mykt.baseResult.RespondsResult;
import com.mykt.constant.LoginConstants;
import com.mykt.constant.SecurityConstants;
import com.mykt.constant.TokenConstants;
import com.mykt.domain.SystemUser;
import com.mykt.exception.MyServiceException;
import com.mykt.system.RemoteUserService;
import com.mykt.util.CurrentUserSecurityUtils;
import com.mykt.utils.md5.MD5Utils;
import com.mykt.utils.ObjectUtils;
import com.mykt.utils.StringUtils;
import com.mykt.utils.token.TokenUtils;
import com.mykt.view.vo.LoginResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

import static com.mykt.utils.token.TokenUtils.createToken;

@Component
public class LoginService {

    @Autowired
    private LoginLogService loginLogService;

    @Autowired
    private RemoteUserService remoteUserService;

    @Autowired
    private RedisService redisService;

    @Value("${token.tokenEffectiveTime}")
    private long tokenEffectiveTime;//token 有效时间 秒

    public LoginResultVo login(String userName, String password){

        if(StringUtils.isAnyBlank(userName,password)){
            loginLogService.recordLoginLog(userName, LoginConstants.LOGIN_FAIL,"账号/密码必填");
            throw new MyServiceException("账号/密码必填");
        }

        if(userName.length() < LoginConstants.USERNAME_MIN_LENGTH || userName.length() > LoginConstants.USERNAME_MAX_LENGTH){
            loginLogService.recordLoginLog(userName, LoginConstants.LOGIN_FAIL,"账号不在指定范围内");
            throw new MyServiceException("账号不在指定范围内");
        }

        if(password.length() < LoginConstants.PASSWORD_MIN_LENGTH || password.length() > LoginConstants.PASSWORD_MAX_LENGTH){
            loginLogService.recordLoginLog(userName, LoginConstants.LOGIN_FAIL,"密码不在指定范围内");
            throw new MyServiceException("密码不在指定范围内");
        }

        //获取用户信息
        RespondsResult<SystemUser> userInfo = remoteUserService.info(userName, SecurityConstants.INNER);
        SystemUser systemUser = userInfo.getData();


        if(ObjectUtils.isNull(systemUser)){
            loginLogService.recordLoginLog(userName, LoginConstants.LOGIN_FAIL,"用户名不存在");
            throw new MyServiceException("用户名不存在");
        }

        //密码校验
        if(!MD5Utils.validPassword(password,systemUser.getPassword())){
            loginLogService.recordLoginLog(userName, LoginConstants.LOGIN_FAIL,"密码错误");
            throw new MyServiceException("密码错误");
        }


        //用户信息
        Map<String,String> userMap=new HashMap<>();
        userMap.put("userId",systemUser.getId());
        userMap.put("userName",userName);

        //生成token
        String token = createToken(userMap);

        //token缓存在redis中
        redisService.setCacheObject(TokenConstants.TOKEN_KEY_PREFIX + token,token,tokenEffectiveTime);
        return new LoginResultVo(systemUser.getId(),systemUser.getUserName(),token,tokenEffectiveTime);
    }



}
